/*! \file
 * \brief Enter a brief description here.
 *
 * Enter a detailed description here. (optional)
 * \author Author (Ivo Wingelaar)
 * \date Date (2011)
 * \version Version (0.0.1)
 * \copyright Copyright (GNU LGPL v3)
 */

#ifndef __LYNX_WINDOW_MANAGER_LINUX_C_HPP_INCLUDED__
#define __LYNX_WINDOW_MANAGER_LINUX_C_HPP_INCLUDED__

#include "LynxWindowManagerStub_C.hpp"

#ifdef _LYNX_LINUX_

#include <GL/gl.h>
#define GLX_GLXEXT_LEGACY 1
#include <GL/glx.h>
#include <GL/glxext.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

#include <X11/extensions/xf86vmode.h>

namespace Lynx
{

class WindowManagerLinux_C : public WindowManagerStub_C
{
public:
    //! constructor
    WindowManagerLinux_C(const WindowParameters& parameters);

    //! destructor
    virtual ~WindowManagerLinux_C();

    //! runs the device. Returns false if device wants to be deleted
    virtual bool run();

    //! notifies the device that it should close itself
    virtual void close();

    //! Cause the device to temporarily pause execution and let other processes to run
    virtual void yield();

    //! Pause execution and let other processes to run for a specified amount of time.
    virtual void sleep(uint32_t timeMs, bool pauseTimer);

    virtual void set_window_caption(std::wstring caption);

    virtual std::wstring get_window_caption(void) const;

    virtual bool is_window_focused(void) const;

    virtual bool is_window_fullscreen(void) const;

    virtual bool set_gamma_ramp(float red, float green, float blue, float brightness, float contrast);

    virtual bool get_gamma_ramp(float& red, float& green, float& blue, float& brightness, float& contrast);

    virtual const char* get_text_from_clipboard() const;

    virtual void copy_to_clipboard(const char* text) const;

    virtual void clear_system_messages(void);

private:
    //! create the driver
    void create_driver();

    bool create_window();

    void create_keymap();

    void init_x_atoms();

    bool switch_to_fullscreen(bool reset=false);

    friend class CursorManagerLinux_C;

    friend class VideoManager_C;

    Display* mDisplay;
    XVisualInfo* mVisual;
    int mScreennr;
    Window mWindow;
    XSetWindowAttributes mAttributes;
    XSizeHints* mStdHints;
    XImage* mSoftwareImage;
    mutable std::string mClipboard;

    XF86VidModeModeInfo mOldVideoMode;

    GLXWindow mGlxWin;
    GLXContext mContext;
    uint32_t mWidth, mHeight;
    bool mWindowHasFocus;
    bool mWindowMinimized;
    bool mUseXVidMode;
    bool mUseXRandR;
    bool mUseGLXWindow;
    int mAutorepeatSupport;

    struct SKeyMap
    {
        SKeyMap()
        {
            //
        }

        SKeyMap(int32_t x11, int32_t win32) :
            mX11Key(x11),
            mWin32Key(win32)
        {
            //
        }

        KeySym mX11Key;
        int32_t mWin32Key;

        bool operator<(const SKeyMap& o) const
        {
            return mX11Key<o.mX11Key;
        }

        bool operator==(const SKeyMap& o) const
        {
            return mX11Key == o.mX11Key;
        }
    };

    std::vector<SKeyMap> mKeyMap;
};

} // namespace Lynx

#endif // _LYNX_LINUX_

#endif // __LYNX_WINDOW_MANAGER_LINUX_C_HPP_INCLUDED__

